using System.Web.UI.WebControls;

using Impact.Model.Objects;
using Impact.Model.Service;
using Impact.Web.Foundation;
using Impact.Model.Services;

namespace Impact.Web.Admin
{
    public partial class Logs : PageBase
    {

        /// <summary>
        /// override of Bind to perform binding specific to this page
        /// </summary>
        protected override void Bind()
        {
            base.Bind();

            LoggingLogCollection entries = 
                new LoggingLogCollection().OrderByDesc( LoggingLog.Columns.Timestamp ).Load();

            logGridView.DataSource = entries;
            logGridView.DataBind();

            int start = 0;
            int end = 0;
            if (entries.Count > logGridView.PageSize)
            {
                start = logGridView.PageIndex * logGridView.PageSize + 1;
                end = start + logGridView.PageSize - 1;
            }
            else if (entries.Count > 0)
            {
                start = logGridView.PageIndex * logGridView.PageSize + 1;
                end = entries.Count;
            }

            summary.Text = string.Format("{0}-{1} of {2} entries.", start, end, entries.Count);

        }

        /// <summary>
        /// event handler fired when the user clicks a new page on the grid
        /// </summary>
        protected void LogGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            logGridView.PageIndex = e.NewPageIndex;
            Bind();
        }

        /// <summary>
        /// event handler fired when the user clicks the 'clear log' button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ClearLogCommand(object sender, CommandEventArgs e)
        {
            Procedures.ClearLogs().Execute();
            Bind();
        }

    }
}
